package com.wetool.wechat.util;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MessageUtil {
    public static Map<String, Object> convert(HttpServletRequest request) throws IOException {

        InputStream in = request.getInputStream();
        HashMap<String, Object> map = new HashMap<>();
        SAXReader saxReader = new SAXReader();
        Document documnet = null;
        try {
            documnet = saxReader.read(in);
        } catch (DocumentException e) {
            e.printStackTrace();
            return null;
        }

        Element element = documnet.getRootElement();

        List<Element> elementList = element.elements();

        // 遍历所有子节点
        for (Element e : elementList)
            map.put(e.getName(), e.getText());
        // 释放资源
        in.close();
        return map;
    }
}
